/*
 * @Author: RAY_HUI 1541917867@qq.com
 * @Date: 2025-07-17 17:41:20
 * @LastEditors: RAY_HUI 1541917867@qq.com
 * @LastEditTime: 2025-08-10 11:26:06
 * @FilePath: \website-reconstruction---vue\vite.config.ts
 * @Description:
 *
 * Copyright (c) 2025 by ${git_name_email}, All Rights Reserved.
 */
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'

export default defineConfig(({ mode }) => {
  // 加载环境变量，只包括以 VITE_ 开头的变量
  const env = loadEnv(mode, process.cwd(), 'VITE_')

  /*   console.log('当前模式:', mode)
  console.log('环境变量:', env) */

  const proxy = {}
  if (env.VITE_APP_BASE_API) {
    //console.log(import.meta.env.VITE_APP_BASE_API)
    // 输出：/dev-api

    proxy[env.VITE_APP_BASE_API] = {
      target: 'http://localhost:8080',
      changeOrigin: true,
      secure: false,
      rewrite: (path) => {
        const before = path
        const after = path.replace(new RegExp(`^${env.VITE_APP_BASE_API}`), '')
        //console.log(`[Proxy] 请求路径替换: ${before} → ${after}`)
        return after
      },
    }
  }
  // 添加静态资源代理
  proxy['/resource'] = {
    target: 'http://localhost:8080',
    changeOrigin: true,
    secure: false,
    rewrite: (path) => path.replace(/^\/resource/, '/resource'),
  }
  return {
    plugins: [vue()],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
      },
    },
    server: {
      proxy,
    },
  }
})
